$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'lanling/allsubject/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '科目名称', name: 'subjectName', index: 'subject_name', width: 80, align: "center"},
            {
                label: '科目类型',
                name: 'typeId',
                index: 'type_id',
                width: 80,
                align: "center",
                formatter: function (value) {
                    if (value === 1) {
                        return '<span class="label label-success">人社类</span>';
                    } else if (value === 2) {
                        return '<span class="label label-warning">交通类</span>';
                    } else if (value === 3) {
                        return '<span class="label label-info">安监类</span>';
                    } else if (value === 0) {
                        return '<span class="label label-default">默认题库</span>';
                    } else {
                        return '<span class="label label-danger">未知</span>';
                    }

                }
            },
            {label: '证件名称', name: 'certificateName', index: 'certificate_name', width: 80, align: "center"},
            { label: '复审时间（年）', name: 'reviewTime', index: 'review_time', width: 80, align: "center" },
            { label: '有效期（年）', name: 'validDate', index: 'valid_date', width: 80, align: "center" },
            {label: '创建时间', name: 'createDate', index: 'create_date', width: 80, align: "center"},
            {label: '修改时间', name: 'lastModifyDate', index: 'last_modify_date', width: 80, align: "center"}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        allSubject: {
            typeId: '1',
            reviewTime: 0,
            validDate: 3
        },
        ruleValidate: {
            subjectName: [
                {required: true, message: '科目名称不能为空', trigger: 'blur'},
                {max: 100, message: '请输入最多100位'}
            ],
            typeId: [
                {required: true, message: '科目类型不能为空', trigger: 'blur'}
            ],
            certificateName: [
                {required: true, message: '证件名称不能为空', trigger: 'blur'},
                {max: 100, message: '请输入最多100位'}
            ]
        },
        q: {
            name: '',
            typeId: ''
        },
        subjectTypeList: []
    },
    mounted() {
        this.getSubjectTypeList();
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.allSubject = {
                typeId: '1',
                reviewTime: 0,
                validDate: 3
            };
            vm.getSubjectTypeList();
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getSubjectTypeList();
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.allSubject.id == null ? "lanling/allsubject/save" : "lanling/allsubject/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(vm.allSubject),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "lanling/allsubject/delete",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "lanling/allsubject/info/" + id, function (r) {
                vm.allSubject = r.allSubject;
                vm.allSubject.typeId = vm.allSubject.typeId + '';
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'name': vm.q.name,
                    'typeId': vm.q.typeId
                },
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getSubjectTypeList: function () {
            $.get("../../sys/dict/queryListByType?type=subject_type", function (r) {
                vm.subjectTypeList = r.list;
            });
        }
    }
});